using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Data.OracleClient;

namespace Kenly.DBFramework
{
    /// <summary>
    /// ParameterBuilder 
    /// </summary>
    internal static class ParameterBuilder
    {

        public static IDataParameter BuildParameter(DatabaseType databaseType, string parameterName, object parameterValue, bool isNullable)
        {
            if (string.IsNullOrEmpty(parameterName))
            {
                throw new DBException(MsgResource.InvalidArguments, new ArgumentNullException("parameterName"));
            }

            IDataParameter parameter = null;
            switch (databaseType)
            {
                case DatabaseType.SqlServer:
                    SqlParameter tempSqlParameter = new SqlParameter(parameterName, parameterValue);
                    parameter = new SqlParameter(tempSqlParameter.ParameterName, tempSqlParameter.SqlDbType, tempSqlParameter.Size, tempSqlParameter.Direction,
                        isNullable, tempSqlParameter.Precision, tempSqlParameter.Scale,
                        tempSqlParameter.SourceColumn, tempSqlParameter.SourceVersion, tempSqlParameter.Value);
                    break;
                case DatabaseType.Oledb:
                    OleDbParameter tempOleParameter = new OleDbParameter(parameterName, parameterValue);
                    parameter = new OleDbParameter(tempOleParameter.ParameterName, tempOleParameter.OleDbType, tempOleParameter.Size, tempOleParameter.Direction,
                        isNullable, tempOleParameter.Precision, tempOleParameter.Scale,
                        tempOleParameter.SourceColumn, tempOleParameter.SourceVersion, tempOleParameter.Value);
                    break;
                case DatabaseType.Oracle:
                    OracleParameter tempOracleParameter = new OracleParameter(parameterName, parameterValue);
                    parameter = new OracleParameter(tempOracleParameter.ParameterName, tempOracleParameter.OracleType, tempOracleParameter.Size, tempOracleParameter.Direction,
                        isNullable, 0, 0,tempOracleParameter.SourceColumn, tempOracleParameter.SourceVersion, tempOracleParameter.Value);
                    break;
                default:
                    break;
            }
            return parameter;
        }


       /// <summary>
       /// 
       /// </summary>
       /// <param name="databaseType"></param>
       /// <param name="parameterName"></param>
       /// <param name="parameterValue"></param>
       /// <returns></returns>
        public static IDataParameter BuildParameter(DatabaseType databaseType, string parameterName, object parameterValue)
        {
            return BuildParameter(databaseType, parameterName, parameterValue, true);
        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="databaseType"></param>
        /// <param name="parameterNames"></param>
        /// <param name="parameterValues"></param>
        /// <param name="isNullable"></param>
        /// <returns></returns>
        public static List<IDataParameter> BuildParameters(DatabaseType databaseType, string[] parameterNames, object[] parameterValues, bool isNullable)
        {
            if(parameterNames.Length!=parameterValues.Length)
            {
                throw new DBException(MsgResource.InvalidArguments, new ArgumentNullException("parameterNames"));
            }

            List<IDataParameter> parameters = new List<IDataParameter>();
            for (int i = 0; i < parameterNames.Length; i++)
            {
                parameters.Add(BuildParameter(databaseType, parameterNames[i], parameterValues[i], isNullable));
            }

            return parameters;
        }

        public static List<IDataParameter> BuildParameters(DatabaseType databaseType, string[] parameterNames, object[] parameterValues)
        {
            return BuildParameters(databaseType, parameterNames, parameterValues, true);
        }

    }


}
